//待补充
$NG.AllReady(function (
    editPage,
    { useAction, useValuesChange, useUpdateRow, useDataIndexChange, useClick }
) {
    const headerTable = $NG.getCmpApi("p_form_ywgcwb_jfd")
    const _d_table = $NG.getCmpApi("p_form_ywgcwb_jfd_d")
    const dgrid3 = $NG.getCmpApi("p_form_ywgcwb_jfd_d2")
    // 监听项目选择
    filterCntMain()
    useValuesChange(({args})=>{
        console.log(`项目选择监听信息`, args);
        $NG.updateState((updater)=>{
            updater.data.p_form_ywgcwb_jfd.setProps({
                u_fbht_name:"",
                u_fbht_no:""
            })
        })
        filterCntMain()
        getProject()
        getFxTzDetail()
    }, "p_form_ywgcwb_jfd.phid_pc")
    useValuesChange(({args})=>{
        getFxTzDetail()
    },"p_form_ywgcwb_jfd.u_sjfdw")
    //监听合同编号的变化
    useValuesChange(({args})=>{
        console.log(`监听合同变化信息`, args);
        const bill_name = args[0].u_fbht_name.origin.bill_no
        $NG.updateState((updater)=>{
            updater.data.p_form_ywgcwb_jfd.setProps({
                u_fbht_no:bill_name
            })
        })
        setTimeout(()=>{
            getCntInfo()
        },200)
    },"p_form_ywgcwb_jfd.u_fbht_name")
    // 监听奖罚明细表
    useUpdateRow(({args})=>{
        console.log(`奖罚明细表监听`,args);
        const _temp = (args[0].u_sl?args[0].u_sl:0)*(args[0].u_hs_prc?args[0].u_hs_prc:0)
        args[0].u_hs_amt=_temp
        args[0].u_ws_amt=_temp/(1+args[0].u_mxsl)
        _d_table.refreshView()
    },"p_form_ywgcwb_jfd_d")
    //根据项目筛选合同
    function filterCntMain(){
        console.log(`项目id`,headerTable.getItem("phid_pc").getValue());
        if(!headerTable.getItem("phid_pc").getValue()) return 
        $NG.updateUI((updater, state) => {
            console.log('过滤参数检查', updater);
            updater.form.p_form_ywgcwb_jfd.u_fbht_name.setProps({
                onBeforeOpen: async () => {
                    var phid_pc = $NG.getCmpApi('p_form_ywgcwb_jfd').getItem('phid_pc').getValue();
                    if (!phid_pc) {
                        $NG.alert("请先选择项目");
                        return false;
                     } else {
                        return true;
                    }
                },
                ...(!!headerTable.getItem("phid_pc").getValue() && {
                    clientSqlFilter: {
                        bill_type: 4,//合同类型
                        phid_pc: headerTable.getItem("phid_pc").getValue()
                    }
                }),
            });
        });
    }
    getProject()
    // 获取项目信息
    function getProject(){
        $NG.execServer("getPtInfoByPhid", {phid:headerTable.getItem('phid_pc').getValue()},function(res){
            if(res.count ==0) return
            const data = JSON.parse(res.data)[0].extendObjects
            console.log(`项目信息获取`,data);
            $NG.updateState((updater)=>{
                updater.data.p_form_ywgcwb_jfd.setProps({
                    u_pc_no:data.pc_no
                })
            })
        })
    }
    //合同信息获取
    function getCntInfo(){
        $NG.execServer("getCntMInfoByPhid", {phid: headerTable.getItem("u_fbht_name").getValue()}, function (res){
            const data= JSON.parse(res.data)[0].extendObjects
            console.log(`合同信息获取`, data);
            $NG.updateState((updater)=>{
                updater.data.p_form_ywgcwb_jfd.setProps({
                    u_sjfdw:{
                        label:data.supply_name,
                        value: data.supply_id
                    }
                })
            })
        })
    }
    //获取台账明细
    function getFxTzDetail(){
        if(!headerTable.getItem("phid_pc").getValue()||!headerTable.getItem("u_sjfdw").getValue()) return
        $NG.execServer("getRiskInfo",{phid_pc:headerTable.getItem("phid_pc").getValue(),gys:headerTable.getItem("u_sjfdw").getValue()},(res)=>{
            console.log(`台账查询`,res);
            if(res.count == 0){return }
            const _data = JSON.parse(res.data)
            dgrid3.clearRows().then(()=>{
                const _tempData = []
                _data.forEach(item=>{
                    const _item = item.extendObjects
                    _tempData.push({
                        u_wt_no:_item.bill_no,
                        u_fxwtsb_time:_item.u_put_dt,
                        u_tb_date:_item.bill_dt,
                        u_fxwt_type:_item.u_risk_problem_cla,//TODO
                        u_xfwtms:_item.u_risk_problem_desc,
                        u_zrzt:_item.u_response_body,
                        u_zrzt_EXName:_item.u_name,
                        u_gys_name:_item.phid_supply_ent,
                        u_gys_name_EXName:_item.compname,
                        u_wtyzcd:_item.u_problem_degree,
                        u_yyfx:_item.u_reason,
                        // u_wtyx:_item.,//TODO问题影响
                        u_plan_jjcs:_item.u_plan_solve,//计划解决措施
                        u_plan_jj_date:_item.u_jhjj_dt,
                        // u_sjjj_date:_item.,//TODO实际解决日期
                        u_fxdj:item.u_fxdj
                    })
                })
                dgrid3.addRows(_tempData)
            })
        })
    }
});